package com.yyh.synctool.client.controller;

import com.yyh.common.Common;
import com.yyh.result.JsonResult;
import com.yyh.synctool.client.component.Setting;
import com.yyh.synctool.client.pojo.vo.CredentialParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 凭证管理
 * @author xuan
 * @create 2021/12/18
 */
@RestController
@RequestMapping("/credential")
public class CredentialController
{
    private Setting setting;

    @Autowired
    public CredentialController(Setting setting) {
        this.setting = setting;
    }

    /** 展示数据库连接配置 */
    @GetMapping("/show-credential")
    public JsonResult showCredential() {
        HashMap<String, Object> config = new HashMap<>();
        config.put("gsid", setting.getGsid());
        config.put("credential", setting.getCredential());

        return JsonResult.getSuccess(config);
    }

    /** 保存数据库连接配置 */
    @PostMapping("/save-credential")
    public JsonResult saveConnection(@RequestBody CredentialParam credentialParam) {
        Map<String, String> config = Common.stringifyMapValue(Common.toMap(credentialParam));
        setting.setGsid(config.get("gsid"))
                .setCredential(config.get("credential"));
        setting.saveCredentialConfig();
        return JsonResult.getSuccess(config);
    }
}
